import { Injectable } from '@nestjs/common';
import { Student, Work } from '@prisma/client';
import { PrismaService } from 'src/prisma/prisma.service';
import { MediaEntity } from './media.entity';

@Injectable()
export class MediaService {
    constructor(
        private readonly prismaService: PrismaService
    ) { }

    async createMedia(work: Work, student: Student) {
        const media: MediaEntity = await this.prismaService.media.findFirst({
            where: {
                work: {
                    id: work.id
                },
                student: {
                    id: student.id
                }
            }
        })
        if (!!media) {
            media.exists = true;
            return media;
        }
        return await this.prismaService.media.create({
            data: {
                work: {
                    connect: {
                        id: work.id
                    }
                },
                student: {
                    connect: {
                        id: student.id
                    }
                }
            }
        })
    }
}
